<template>
  <div class="material" :id="'Material' + singleMaterial.id">
    <header>
      <section class="material-name">
        <van-checkbox v-model="singleMaterial.checked" checked-color="#548AF1" style="margin-right:5px;" v-if="globalState === 0"></van-checkbox>
        <span>{{ index + 1 }}</span>
        <span class="line"></span>
        <span>{{ singleMaterial.materialEntity && singleMaterial.materialEntity.name }}</span>
      </section>
      <section v-show="singleMaterial.materialEntity&&singleMaterial.materialEntity.isBatchMag" class="batch-label">
        <img src="@/assets/images/batch.png" alt="" />
        <div>批次</div>
      </section>
    </header>
    <main>
      <section class="material-code">
        <span>{{ singleMaterial.materialEntity && singleMaterial.materialEntity.code }} </span>
        <section class="warehouse-num">
          <section class="num1">
            <span>计划入库数</span>
            <span>{{ singleMaterial.planAmount }}</span>
          </section>
          <section class="num2">
            <span>{{ globalState === 0 ? '待入库数' : globalState === 1 ? '已入库数' : '未知' }}</span>
            <span>{{singleMaterial.actualAmount < 0 ? 0: singleMaterial.actualAmount}}</span>
          </section>
        </section>
      </section>
      <section class="material-info">
        <section class="sku" v-if="singleMaterial.skuName">
          <label>特征参数：</label>
          <span style="font-size: 12px;">{{singleMaterial.skuName}}</span>
        </section>
        <section class="sku" >
          <label>快捷码：</label>
          <span style="font-size: 12px;">{{singleMaterial.materialEntity?singleMaterial.materialEntity.customFieldOne:''}}</span>
        </section>
        <section class="sku">
          <label>规格：</label>
          <span style="font-size: 12px;">{{singleMaterial.materialEntity?singleMaterial.materialEntity.standard:''}}</span>
        </section>
        <section class="sku" style="margin:5px 0">
          <div style="width:50%"><label>物料单位：</label><span style="font-size: 12px;">{{singleMaterial.materialEntity?singleMaterial.materialEntity.comp:''}}</span></div>
          <div style="width:50%" v-if="singleMaterial.materialEntity && singleMaterial.materialEntity.isDoubleUnit">
            <label>计量单位：</label>
            <span style="font-size: 12px;">{{singleMaterial.materialEntity?singleMaterial.materialEntity.unit:''}}</span>
          </div>
        </section>
        <section class="sku" v-if="!singleMaterial.materialEntity.isBatchMag">
          <label>实际数量：</label>
          <el-input size="mini" type="number" v-model="singleMaterial.realNum" :disabled="globalState !== 0"></el-input>
        </section>
        <!-- <section class="sku">
          <label>供应商：</label>
          <span style="font-size: 12px;">{{ singleMaterial.supplierName }}</span>
        </section> -->
        <section class="sku">
          <label>扎数：</label>
          <el-input size="mini" type="number" v-model="singleMaterial.extendMap.zhashu" :disabled="globalState !== 0"></el-input>
        </section>
        <section class="sku">
          <label>备注：</label>
          <el-input size="mini" v-model="singleMaterial.materialRemark" :disabled="globalState !== 0"></el-input>
        </section>
        <section class="sku">
          <label>仓库：</label>
          <el-select filterable v-model="singleMaterial.warehouseInfo.warehouseId" :disabled="globalState !== 0" placeholder="请选择" size="mini" style="width:100%;">
            <el-option v-for="item in singleMaterial.warehouseInfo.warehouseList" :label="item.warehouseName" :value="item.warehouseId" :key="item.warehouseId"></el-option>
          </el-select>
        </section>
      </section>
      <!-- 批次 -->
      <template v-if="singleMaterial.materialEntity.isBatchMag">
        <section class="sku" >
            <label>批次信息：</label>
            <el-button type="text" size="mini" @click="addMaterialBeachInfo" :disabled="globalState !== 0">添加批次信息</el-button>
          </section>
        <div v-show="singleMaterial.materialEntity.isBatchMag && singleMaterial.isExpand">
          <el-table :data="singleMaterial.stockMaterialDetaiList" ref="multipleTable" style="width: 100%;" border @select="tableHandlerSelect" @select-all="tableHandlerSelectAll" class="content-table-item">
              <el-table-column prop="" label="" type="selection" width="40" align="center"></el-table-column>
              <el-table-column prop="" label="序号" type="index" width="50" align="center"></el-table-column>
              <el-table-column prop="" label="批次号" width="" align="center">
                  <template slot-scope="{ row, $index }">
                    <el-input v-model="row.barCode" size="mini" :disabled="globalState !== 0 || Boolean(row.stockMaterialId)" placeholder="输入批次号"></el-input>
                  </template>
              </el-table-column>
              <el-table-column prop="" label="计划数量" width="" align="center">
                  <template slot-scope="{ row, $index }">
                    <el-input :value="row.planNum" size="mini" :disabled="globalState !== 0" disabled></el-input>
                  </template>
              </el-table-column>
              <el-table-column prop="" label="实际数量" width="" align="center">
                  <template slot-scope="{ row, $index }">
                    <el-input v-model="row.realNum" type="number" size="mini" :disabled="globalState !== 0" placeholder="实际入库数"></el-input>
                  </template>
              </el-table-column>
              <el-table-column prop="" label="扎数" width="" align="center">
                  <template slot-scope="{ row, $index }">
                    <el-input v-model="row.extendMap.zhashu" size="mini" :disabled="globalState !== 0" @input="inputEvent(row)" placeholder="输入扎数"></el-input>
                  </template>
              </el-table-column>
              <el-table-column prop="" label="操作" width="60">
                  <template slot-scope="{ row, $index }">
                      <el-button type="text" size="mini" @click="deleteMaterialBatchInfo(row,$index)" :disabled="globalState !== 0">删除</el-button>
                  </template>
              </el-table-column>
          </el-table>
        </div>
      </template>
    </main>
    <footer>
      <section class="btns">
        <section @click="handleExpandClick" class="expand-fold">
          <span>{{ singleMaterial.isExpand ? '收起' : '展开' }}</span>
          <img v-if="singleMaterial.isExpand" style="width: 11px" src="@/assets/images/up-icon.png" alt="" />
          <img style="width: 11px" v-else src="@/assets/images/down-icon.png" alt="" />
        </section>
        <section class="action">
          <slot name="action"></slot>
        </section>
      </section>
    </footer>
  </div>
</template>

<script>
import Decimal from "decimal.js";
export default {
  props: {
    singleMaterial: {
      type: Object,
      default: () => ({}),
    },
    index: {
      type: Number,
    },
    globalState:{
      type:Number,
      require:false,
      default:0 //0-添加 1-查看
    }
  },
  watch: {
    singleMaterial: {
      handler(n, o) {
        //console.log(this.singleMaterial, "---2-=======");
      },
      deep: true,
      immediate: true,
    },
    "singleMaterial.stockMaterialDetaiList": {
      handler(n, o) {
        this.batchTableConfirmOrCancelSelected();
      },
      deep: true,
      immediate:true
    },
  },
  methods: {
    addMaterialBeachInfo(){
      const info = {
        checked:false,
        barCode:"",
        planNum:this.singleMaterial.planAmount,
        realNum:"",
        extendMap:{
          zhashu:""
        }
      }
      this.singleMaterial.stockMaterialDetaiList.push(info);
      this.singleMaterial.isExpand = true;
    },
    deleteMaterialBatchInfo(row,index){
      this.singleMaterial.stockMaterialDetaiList.splice(index,1);
      this.calculateTotalZS();
    },

    //批次勾选
    batchTableConfirmOrCancelSelected(){
      if(!this.singleMaterial.materialEntity.isBatchMag){
        return;
      }
      const list = this.singleMaterial?.stockMaterialDetaiList || [];
      for(let i  = 0; i < list.length; i++){
        this.$nextTick(() => {
          this.$refs.multipleTable?.toggleRowSelection(list[i],list[i].checked);
        })
      }
    },
    materialRemarkFocus() {
      this.$emit("updateMaterialRemark", this.singleMaterial);
    },
    handleExpandClick() {
      this.singleMaterial.isExpand = !this.singleMaterial.isExpand;
      this.$emit("updateMaterialExpand", this.singleMaterial);
    },
    tableHandlerSelect(selected,row){
      row.checked = !row.checked;
      this.calculateTotalZS();
    },
    tableHandlerSelectAll(selected){
      if(selected.length === 0){
        const list = this.singleMaterial.stockMaterialDetaiList;
        list.forEach(v => v.checked = false);
      } else {
        selected.forEach(v => v.checked = true);
      }
      this.calculateTotalZS();
    },
    inputEvent(row){
      const inputValue = row.extendMap.zhashu;
      const regex = /^(?!00)\d+(\.\d{0,1})?$/;
      if (!regex.test(inputValue) && inputValue !== '') {
          row.extendMap.zhashu = inputValue.slice(0, -1);
      }
      this.calculateTotalZS();
      //this.$emit("materialInfoChange",this.singleMaterial);
    },
    calculateTotalZS(){
      const list = this.singleMaterial.stockMaterialDetaiList;
      const array = list.filter(v => v.checked);
      let t = 0;
      t = array.reduce((pre,cur) => {
        const n = Number(cur.extendMap.zhashu);
        return pre + ( n >= 0 ? n : 0 );
      },0);
      this.singleMaterial.extendMap.zhashu = t;
    }
  }
};
</script>

<style lang="scss" scoped>
.material {
  background: #ffffff;
  box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.16);
  border-radius: 4px 4px 4px 4px;
  margin-bottom: 8px;
  header {
    background: linear-gradient(90deg, #dce8ff 0%, rgba(255, 255, 255, 0) 100%);
    border-radius: 4px 4px 0px 0px;
    height: 38px;
    display: flex;
    justify-content: space-between;
    .material-name {
      padding-left: 11px;
      font-size: 18px;
      font-family: Source Han Sans CN, Source Han Sans CN;
      font-weight: 800;
      color: #333333;
      display: flex;
      align-items: center;
      .line {
        margin: 0 12px 0 17px;
        display: inline-block;
        width: 1px;
        height: 25px;
        background: linear-gradient(
          180deg,
          rgba(255, 255, 255, 0) 0%,
          #9f9f9f 54%,
          rgba(236, 255, 239, 0) 100%
        );
        border-radius: 0px 0px 0px 0px;
        opacity: 1;
      }
      & > span:last-child {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display: inline-block;
        width: 200px;
      }
    }
    .batch-label {
      position: relative;
      div {
        position: absolute;
        // width: 50%;
        // height: 134%;
        -webkit-transform: rotate(50deg);
        transform: rotate(50deg);
        font-size: 12px;
        font-family: SourceHanSansCN-Regular;
        color: #fff;
        top: 7px;
        right: 2px;
      }
    }
  }
  main {
    padding: 0 5px 7px 5px;
    .material-code {
      height: 32px;
      display: flex;

      justify-content: space-between;
      align-items: center;
      & > span {
        font-size: 16px;
        font-family: Source Han Sans CN, Source Han Sans CN;
        font-weight: 500;
        color: #333333;
      }
      .warehouse-num {
        display: flex;
        .num1,
        .num2 {
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          line-height: 16px;
          span:nth-of-type(1) {
            font-size: 10px;
            font-family: Source Han Sans CN, Source Han Sans CN;
            font-weight: 400;
            color: #333333;
          }
          span:nth-of-type(2) {
            font-size: 12px;
            font-family: Source Han Sans CN, Source Han Sans CN;
            font-weight: 400;
          }
        }
        .num1 {
          width: 64px;
          height: 32px;
          background: rgba(255, 242, 223, 0.52);
          border-radius: 2px 2px 2px 2px;
          opacity: 1;
          border: 1px solid #fbb240;
          margin-right: 4px;
          span:nth-of-type(2) {
            color: #f29a21;
          }
        }
      }
      .num2 {
        width: 64px;
        height: 32px;
        background: #e7ffe6;
        border-radius: 2px 2px 2px 2px;
        opacity: 1;
        border: 1px solid #48ba47;
        span:nth-of-type(2) {
          color: #48ba47;
        }
      }
    }
    .material-info {
      label {
        font-size: 14px;
        font-family: SourceHanSansCN-Regular, SourceHanSansCN-Regular;
        font-weight: normal;
        color: #666666;
      }
      .sku {
        display: flex;
        margin-top: 4px;
        .el-input {
          // width: 16.4375rem;
          ::v-deep {
            .el-input__inner {
              // width: 16.4375rem;
            }
          }
        }
        label {
          display: inline-block;
          width: 6rem;
        }
      }

      .line {
        display: flex;
        justify-content: space-between;
        &:nth-of-type(2) {
          margin: 4px 0;
        }
        .el-input {
          width: 73px;
        }

        .item-right {
          display: flex;
          align-items: center;
          .line-remark {
            position: relative;
            .mask {
              position: absolute;
              top: 0;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .line-tip {
        color: red;
        font-size: 14px;
        margin-bottom: 4px;
        span:nth-of-type(2) {
          font-size: 13px;
        }
      }
    }
    .batchs {
      //   padding: 10px 11px 0 9px;
      margin-top: 10px;

      .table-header {
        font-size: 13px;
        font-family: Source Han Sans CN, Source Han Sans CN;
        font-weight: 400;
        color: #666666;
        display: flex;
        background: #f6f6f6;
        border-radius: 2px 2px 0px 0px;
        height: 28px;
        line-height: 28px;
        & > section {
          width: 25%;
          padding-left: 20px;
          & > span:nth-of-type(2) {
            font-weight: bold;
            color: #666666;
          }
        }
        & > span {
          display: inline-block;
          width: 25%;
        }
        & > span:nth-of-type(2) {
          padding-left: 8px;
        }
        & > span:nth-of-type(3) {
          padding-left: 8px;
        }
      }
      .table-body {
        .tr {
          font-size: 13px;
          font-family: Source Han Sans CN, Source Han Sans CN;
          font-weight: 400;
          color: #666666;
          display: flex;
          background: #f6f6f6;
          border-radius: 2px 2px 0px 0px;
          min-height: 28px;
          line-height: 28px;
          align-items: center;

          & > section {
            width: 40%;
            display: flex;
            align-items: center;
            & > div {
              font-weight: bold;
              color: #666666;
              // margin-left: 5px;
              word-wrap: break-word;
            }
            & > span:nth-child(1) {
              margin-left: 5px;
            }
          }

          & > span:nth-of-type(1),
          & > span:nth-of-type(2),
          & > span:nth-of-type(3) {
            width: 20%;
          }
          span:nth-of-type(2),
          span:nth-of-type(3) {
            padding-left: 8px;
          }
        }
      }
    }
  }
  footer {
    height: 45px;
    padding: 0 7px 0 4px;
    .btns {
      border-top: 1px solid #ebebeb;
      height: 100%;
      display: flex;
      justify-content: space-between;
      padding: 0 5;
      .expand-fold {
        display: flex;
        align-items: center;
        & > span {
          font-size: 14px;
          font-family: Source Han Sans CN, Source Han Sans CN;
          font-weight: 400;
          color: #999999;
        }
        img {
          margin-left: 6px;
        }
      }
      .action {
        display: flex;
        align-items: center;
        .el-button {
          // border: 1px solid #548af1;
          // padding: 6px 6px 8px 6px;
          padding: 0;
          width: 64px;
          height: 32px;
          line-height: 32px;
          text-align: center;
        }
      }
    }
  }
}
::v-deep .el-table__header th {
  padding: 0; /* 调整上下内边距 */
}
::v-deep .el-table__row {
  margin-bottom: 15px; /* 调整这个值来改变行间距 */
}
::v-deep .el-table .el-table__cell{
  padding: 0;
}
</style>
